package UI;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

import DBTasks.Donation;
import DBTasks.LargestMonthlyDonations;

@SuppressWarnings("serial")
public class DonationsPanel extends JPanel implements ActionListener{

	private JButton returnButton;
	private DefaultTableModel model;
	private JTable table;
	private JTableHeader headers;
	private MainFrame frame;
	private static String calendarDate;
	
	@SuppressWarnings("deprecation")
	public DonationsPanel(MainFrame mainFrame) {
		
		this.setSize(950, 600);
		this.frame = mainFrame;
		
		JLabel logo = new JLabel("Atlanta Pet Adoption Consortium");
		logo.setFont(new Font("Tahoma", Font.BOLD, 30));
		
		String shelterName = (String) frame.getLogin().getShelterList().getSelectedItem();
		JLabel shelterLabel = new JLabel("Largest Monthly Donations for " + shelterName);
		shelterLabel.setFont(new Font("Tahoma", Font.PLAIN, 18));
		
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		calendarDate = dateFormat.format(date);
		
		JLabel dateLabel = new JLabel("Date: " + calendarDate);
		dateLabel.setFont(new Font("Tahoma", Font.PLAIN, 18));
		
		returnButton = new JButton("Return");
		returnButton.addActionListener(this);
		
		String[] columnNames = {"Month", "Name", "Email", "Phone", "City", "Amount"};
		Map<Integer, List<Donation>> donations = LargestMonthlyDonations.getLargestMonthlyDonations(shelterName);
		ArrayList<Object[]> data = new ArrayList<Object[]>();
		
		for(int i = 0; i < date.getMonth()+1; i++) {
			if(donations.containsKey(i+1)) {
				List<Donation> donationList = donations.get(i+1);
				Object[] entry = new Object[6];
				entry[0] = frame.getMonth(i);
				entry[1] = donationList.get(0).getName();
				entry[2] = donationList.get(0).getEmail();
				entry[3] = donationList.get(0).getPhoneNumber();
				entry[4] = donationList.get(0).getCity();
				entry[5] = "$" + donationList.get(0).getAmount();
				data.add(entry);
				if(donationList.size() > 0) {
					for(int j = 1; j < donationList.size(); j++) {
						Object[] otherEntry = new Object[6];
						otherEntry[0] = "";
						otherEntry[1] = donationList.get(j).getName();
						otherEntry[2] = donationList.get(j).getEmail();
						otherEntry[3] = donationList.get(j).getPhoneNumber();
						otherEntry[4] = donationList.get(j).getCity();
						otherEntry[5] = "$" + donationList.get(j).getAmount();
						data.add(otherEntry);
					}
				}
			}
			else {
				Object[] entry = new Object[6];
				entry[0] = frame.getMonth(i);
				entry[1] = "-----";
				entry[2] = "-----";
				entry[3] = "-----";
				entry[4] = "-----";
				entry[5] = "-----";
				data.add(entry);
			}
		}
		
		Object[][] actualData = new Object[data.size()][6];
		for(int i = 0; i < actualData.length; i++) {
			Object[] object = data.get(i);
			actualData[i][0] = object[0].toString();
			actualData[i][1] = object[1].toString();
			actualData[i][2] = object[2].toString();
			actualData[i][3] = object[3].toString();
			actualData[i][4] = object[4].toString();
			actualData[i][5] = object[5].toString();
		}
		model = new DefaultTableModel(actualData, columnNames);
		table = new JTable(model);
		table.setGridColor(Color.black);
		headers = table.getTableHeader();
		table.setBorder(BorderFactory.createLineBorder(Color.black));
		headers.setBorder(BorderFactory.createLineBorder(Color.black));
		table.setRowHeight(30);
		
        GroupLayout gl = new GroupLayout(this);
		
		gl.setHorizontalGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)
				.addComponent(logo)
				.addGroup(gl.createSequentialGroup()
						.addComponent(shelterLabel)
						.addGap(500)
						.addComponent(dateLabel))
				.addComponent(headers)
				.addComponent(table)
				.addGroup(gl.createSequentialGroup()
						.addGap(850)
						.addComponent(returnButton)));
		
	    gl.setVerticalGroup(gl.createSequentialGroup()
	    		.addGap(15)
			    .addComponent(logo)
			    .addGap(40)
			    .addGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)
						.addComponent(shelterLabel)
						.addComponent(dateLabel))
			    .addGap(20)
			    .addComponent(headers)
			    .addComponent(table)
			    .addGap(20)
			    .addComponent(returnButton)
			    .addGap(40)
	    );
		
		gl.setAutoCreateContainerGaps(true);
		
		this.setLayout(gl);
		
	}
	
	public void paintComponent(Graphics g) {
        super.paintComponents(g);
        g.setColor(Color.blue);
        g.fillRect(0, 0, this.getWidth(), 70);
    }

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource().equals(returnButton)) {
			frame.setPanel(frame.getHomepage());
		}
	}

}
